<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'

import { ref } from 'vue'

//文章分类数据模型
// const categorys = ref([
//   {
//     id: "",
//     name: "",
//   },
// ]);
const name = ref('')
const phone = ref('')
const readerId = ref('')

//用户搜索时选中的状态id
// const categoryId=ref('')

//用户搜索时选中的发布状态
const state=ref('')

//读者列表数据模型
const reader = ref([
    {
        "id": 5,
        "name": "姓名",
        "idNumber": "bianhao",
        "phone": "12345678910",
        "balance": "1.0",
        "status": 0,
        // "state": status===0 ? '启用' : '禁用'
    }
])

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(10)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    readerList();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
    readerList();
}

// import {
//   bookClassifyListService,
//   bookClassifyByNameService,
//   bookListService,
//   addBookService,
//   updateBookService,
//   bookDeleteService
// } from "@/api/books.js";

import { readerListService, addReaderService,updateReaderService } from '@/api/admin.js'

//回显文章分类
// const bookClassifyList = async()=>{
//     let result = await bookClassifyListService();
//     categorys.value = result.data;

// }

//获取读者列表数据
const readerList = async()=>{
    let params ={
        pageNum: pageNum.value,
        pageSize: pageSize.value,
        name:name.value ? name.value : null,
        state: state.value ? state.value : null,
        phone: phone.value ? phone.value : null,
        readerId: readerId.value ? readerId.value : null

    }
    let result = await readerListService(params);

    //渲染视图
    total.value = result.data.total;
    reader.value = result.data.items;

    //处理数据，状态
    for(let j=0;j<reader.value.length;j++){
        if(reader.value[j].status==0){
            reader.value[j].status = '启用';
        }else{
            reader.value[j].status = '禁用';
        }
    }

}

// bookClassifyList();
readerList();


import {Plus} from '@element-plus/icons-vue'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//定义变量，控制标题的展示
const title = ref("")


//添加表单数据模型
const readerModel = ref({
    id: '',
    name: '',
    idNumber: '',
    phone: '',
    balance:'',
    status: ''
})
//添加表单校验
const rules = {
    name: [{ required: true, message: "请输入姓名", trigger: "blur" },
            {min:1, max: 20, message: '长度不超过20位非空字符', trigger: 'blur'}],
    idNumber: [{ required: true, message: "请输入编号", trigger: "blur" },
            {min:1,max: 10, message: '长度不超过10位非空字符', trigger: 'blur'}],
    phone: [
        {   required: true, message: '请输入手机号码', trigger: 'blur' },
        {   pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
            message: '请正确填写手机号码',
            trigger: 'blur' 
        }
    ]

};

//清空模型的数据
const clearDate = () => {

//   bookModel.value = "";
  readerModel.value.id= '',
  readerModel.value.name='',
  readerModel.value.idNumber= ''
  readerModel.value.phone= '',
  readerModel.value.balance='0.0',
  readerModel.value.status= ''
};

//调用接口，添加表单
import { ElMessage, ElMessageBox } from "element-plus";
const addReader = async () => {

  //调用接口
  let result = await addReaderService(readerModel.value);
  ElMessage.success(result.msg ? result.msg : "添加成功");

  //调用获取所有图书的函数
  readerList();

  visibleDrawer.value = false;
};

//展示编辑弹窗
const showDrawer = (row) => {
  visibleDrawer.value = true;
  title.value = "修改";
  //数据拷贝
  readerModel.value.name = row.name;
  readerModel.value.idNumber = row.idNumber;
  readerModel.value.phone = row.phone;
  readerModel.value.balance = row.balance;
  readerModel.value.status = row.status;
  
  //扩展id属性，将来需要传递给后台
  readerModel.value.id = row.id;
};

//修改读者
const updateReader = async () => {

  //调用接口
  if(readerModel.value.status=='禁用' || readerModel.value.status=='启用'){
    readerModel.value.status = readerModel.value.status=='禁用' ? 1 : 0;
  }
  

  let result = await updateReaderService(readerModel.value);

  ElMessage.success(result.msg ? result.msg : "修改成功");

  //调用获取所有读者的函数
  readerList();

  //隐藏弹窗
  visibleDrawer.value = false;
};



// //删除图书
// const deleteBook = (row)=>{
//   //提示用户 确认框

//   ElMessageBox.confirm(
//     '是否确认删除该分类?',
//     '温馨提示',
//     {
//       confirmButtonText: '确认',
//       cancelButtonText: '取消',
//       type: 'warning',
//     }
//   )
//     .then(async () => {
//       //调用接口
//       let result = await bookDeleteService(row.id);

//       ElMessage({
//         type: 'success',
//         message: '删除成功',
//       })

//       //刷新列表
//       bookList();
//     })
//     .catch(() => {
//       ElMessage({
//         type: 'info',
//         message: '取消删除',
//       })
//     })

// }
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>借阅人管理</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer=true;title = '添加';clearDate();">添加</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        
        <el-form inline >
            <!-- <el-input v-model="input" placeholder="Please input" clearable /> -->
            <el-form-item>
                <el-input v-model="name" placeholder="请输入姓名" clearable />
            </el-form-item>
            <!-- <el-form-item label="图书分类：">
                <el-select placeholder="请选择" v-model="categoryId">
                    <el-option 
                        v-for="c in categorys" 
                        :key="c.id" 
                        :label="c.name"
                        :value="c.id">
                    </el-option>
                </el-select>
            </el-form-item> -->

            
            <el-form-item >
                <el-input v-model="readerId" placeholder="请输入编号" clearable />
            </el-form-item>
            
            <el-form-item>
                <el-input v-model="phone" placeholder="请输入电话号码" clearable />
            </el-form-item >
            <el-form-item label="状态：">
                <el-select placeholder="请选择" v-model="state" >
                    <el-option label="启用" value=0 ></el-option>
                    <el-option label="禁用" value=1></el-option>
                </el-select>
            </el-form-item>
           <br> 
            <el-form-item>
                <el-button type="primary" @click="readerList">搜索</el-button>
                <el-button @click="readerId=''; name='';phone='';state=''; readerList(); ">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 读者列表 -->
        <el-table :data="reader" style="width: 100%">
            <el-table-column label="序号" type="index"> </el-table-column>
            <el-table-column label="姓名" prop="name"></el-table-column>
            <el-table-column label="编号" prop="idNumber"></el-table-column>
            <el-table-column label="电话号码" prop="phone"></el-table-column>
            <el-table-column label="余额" prop="balance"> </el-table-column>
            <el-table-column label="状态" prop="status"></el-table-column>
            <!-- <el-table-column label="地点" prop="address"></el-table-column> -->
            
            <el-table-column label="操作" width="100" >
                <template #default="{ row }">
                    <el-button  type="primary" @click="showDrawer(row)">修改</el-button>
                    <!-- <el-button  type="primary" @click="deleteBook(row)" >{{ status=="0" ? "停用" : "启用"  }}</el-button> -->
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[5 ,10, 15]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />

        <!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="30%">
            <!-- 添加表单 -->
            <el-form :model="readerModel" label-width="100px" :rules="rules" >
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="readerModel.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="电话号码" prop="phone">
                    <el-input v-model="readerModel.phone" placeholder="请输入电话号码"></el-input>
                </el-form-item>
                <el-form-item label="状态：">
                <el-select placeholder="默认为启用" v-model="readerModel.status" >
                    <el-option label="启用" value=0 ></el-option>
                    <el-option label="禁用" value=1></el-option>
                </el-select>
                </el-form-item>

                <el-form-item label="编号" prop="idNumber" >
                    <el-input v-model="readerModel.idNumber" placeholder="请输入编号" :disabled="title == '修改' ? true : false"></el-input>
                </el-form-item>

                <el-form-item label="余额"  >
                    <el-input-number v-model="readerModel.balance" :precision="1" :step="0.1"  />
                </el-form-item>


                <el-form-item>
                    <el-button type="primary" @click="title == '添加' ? addReader() : updateReader()">确认</el-button>
                    <el-button @click="visibleDrawer = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}

/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>